/*
 * @(#)LDAPImportAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.ldap.web;

import java.util.ArrayList;
import java.util.List;
import java.sql.Connection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.web.AbstractAction;
import com.dcivision.ldap.core.Env;
import com.dcivision.ldap.core.LdapImportManager;
/**
  LDAPImportAction.java

  This class is responsible for LDAPImport user actions.

  @author          Charlie Liu
  @company         DCIVision Limited
  @creation date   17/12/2004
  @version         $Revision: 1.11.4.2 $
*/


public class LDAPImportAction extends AbstractAction {
		
  public static final String REVISION = "$Revision: 1.11.4.2 $";
  
  public final static String MESSAGE_DATA_IMPORT = "user.message.data.import"; 

  public ActionForward execute(ActionMapping mapping,
                               ActionForm form, 
					           HttpServletRequest request,
					           HttpServletResponse response)
						       throws Exception {
    try{
      request.setAttribute(GlobalConstant.FUNCTION_CODE_KEY,SystemFunctionConstant.SETUP_IMPORT_DATA);
      LDAPImportForm importForm=(LDAPImportForm)form;
		  HttpSession session=request.getSession();		
		  log.debug("opMode:="+ importForm.getOpMode());
		  
		  if(GlobalConstant.OP_MODE_LIST_RETAIN.equals(importForm.getOpMode())){
		  	importForm.setNavMode(GlobalConstant.NAV_MODE_NEW);
		  	session.removeAttribute("resultList");
		  	return mapping.findForward("N");
		  }
		  
		  if("INS".equals(importForm.getOpMode())){
		  	Env env =new Env();
		  	if(!env.voididateMapping()){
		  		session.removeAttribute("resultList");
		  		ActionError mappingError = new ActionError("user.label.ldap_setup_parameter_error");
		  		addError(request,mappingError);
		  		return mapping.findForward("L");
		  	}			
		  	log.debug("LDAPImportData has begin!");
		  	
		  	//init. LDAP Import Operator API
		  	SessionContainer sessionContainer = (SessionContainer) request.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
		  	Connection conn = (Connection) request.getAttribute(GlobalConstant.DB_KEY);
		  	LdapImportManager oprator=new LdapImportManager(sessionContainer,conn,env);
		  	
		  	//check import / update records
		  	boolean importUserAccountFlag = importForm.getCreateUserFlag().equals("Y");
		  	boolean importUserGroupFlag = importForm.getCreateUserGroupFlag().equals("Y");
		  	
		  	//trigger the import process
		  	List results = oprator.importData(importUserAccountFlag,importUserGroupFlag);
		  	
	  	    importForm.setImportResults(results);
		  	importForm.setNavMode(GlobalConstant.NAV_MODE_LIST);
		  }
		  
      if(GlobalConstant.NAV_MODE_LIST.equals(importForm.getNavMode())){
		  	if(importForm.getImportResults()==null||importForm.getImportResults().isEmpty()){
		  		importForm.setImportResults((ArrayList)session.getAttribute("resultList"));
		  	}else{
		  		session.setAttribute("resultList",importForm.getImportResults());
		  	}			
		  	importForm.setOpMode(GlobalConstant.OP_MODE_LIST_SEARCH);
		  	request.setAttribute("LDAPImportForm",importForm);
		  	//log.debug("pageSum size="+page.length);
		  	return mapping.findForward("L");
		  }		
    }catch(Throwable e){
		  log.error(e);
		  ActionError error= new ActionError(e.getMessage());
		  this.addError(request,error);		
    }	
	  return mapping.findForward("L");
  }
  
  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode(){
  	return SystemFunctionConstant.SETUP_IMPORT_DATA;  	
  }
  
  public String getMajorDAOClassName(){
  	return "";
  }
}